﻿using System.Collections.Generic;
using System.Web.Mvc;
using System.Text;

namespace SubSystem.Helpers
{
    public static class HtmlTagSelector
    {
        public static string TagSelector(this HtmlHelper htmlHelper, IEnumerable<string> Tags)
        {
            var sb = new StringBuilder();

            CreateTagInputDiv(sb);

            CreateTagListDiv(sb, Tags);

            CreateTagsStringDiv(sb, Tags);

            return sb.ToString();
        }

        private static void CreateTagsStringDiv(StringBuilder sb, IEnumerable<string> Tags)
        {
            sb.AppendLine("<div class=\"TagsString\">");
            sb.Append("<input type=\"Hidden\" name=\"Tags\" value=\"");

            foreach (var tag in Tags)
            {
                sb.Append(tag + ",");
            }

            sb.Remove(sb.Length - 1, 1);

            sb.AppendLine("\" />");
            sb.AppendLine("</div>");
        }

        private static void CreateTagListDiv(StringBuilder sb, IEnumerable<string> Tags)
        {
            sb.AppendLine("<div class=\"TagList\">");
            sb.AppendLine("<ul>");
            
            foreach (var tag in Tags)
            {
                sb.AppendLine(string.Format("<li class=\"Tag\">{0}</li>", tag));
            }
        
            sb.AppendLine("</ul>");    
            sb.AppendLine("</div>");
        }

        private static void CreateTagInputDiv(StringBuilder sb)
        {
            sb.AppendLine("<div class=\"TagInput\">");
            sb.Append("<input type=\"Text\" name=\"TagToAdd\" />");
            sb.AppendLine("&nbsp;<span class=\"AddTag\">+</span>");
            sb.AppendLine("</div>");
        }
    }
}
